Skip to content

Fix particleset repr - #3021

Open
erikvansebille wants to merge 3 commits into
Parcels-code:mainfrom
erikvansebille:fix_particleset_repr
Open

erikvansebille wants to merge 3 commits into
Parcels-code:mainfrom
erikvansebille:fix_particleset_repr

Conversation

@erikvansebille

Copy link
Copy Markdown
Member

Description

This PR (re-)implements ParticleSet.__repr__ and ParticleSetView.__repr__. It also adds tests and improves/cleans up formatting

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
    • Describe how you used it (e.g., by pasting your prompt):

CoPilot has helped me with some of the changes to the ParticleSetView.__repr__

def __getitem__(self, index):
"""Get a single particle by index."""
return ParticleSetView(self._data, index=index, pclass=self._pclass)
return ParticleSetView(self._data, index=index, pclass=self._pclass, time_interval=self.fieldset.time_interval)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed in order to print full dates (using parcels.float_to_datelike) for e.g. print(pset[0]). If we don't add the time-Interval, these Particle.t could only be printed as floats.

Do you agree the date-printing is worth the extra complexity of adding another argument to ParticleSetView.__init__?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think injecting FieldSet context into ParticlesetView like this weakens the abstraction of the ParticleSetView (and also is misleading to users, since the data stored in ParticleSetView are floats). Personally I don't think its worth it

Comment thread src/parcels/_repr_utils.py Outdated
time_string = "not_yet_set" if pview.t is None or np.isnan(pview.t) else f"{pview.t:f}"
out = f"P[{pview.particle_id}]: time={time_string}, z={pview.z:f}, y={pview.y:f}, x={pview.x:f}"
vars = [v.name for v in pview._pclass.variables if v.to_write is True and v.name not in ["z", "y", "x", "time"]]
from parcels._core.utils.time import float_to_datelike

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to import here because otherwise a circular import?

E   ImportError: cannot import name 'float_to_datelike' from partially initialized module 'parcels._core.utils.time' (most likely due to a circular import) (/Users/erik/Codes/parcels/src/parcels/_core/utils/time.py)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b444b29

"""Return a pretty repr for Variable"""
return f"Variable(name={var._name!r}, dtype={var.dtype!r}, initial={var.initial!r}, to_write={var.to_write!r}, attrs={var.attrs!r})"
out = f"Variable(name={var._name!r}, dtype={np.dtype(var.dtype).name!r}, initial={var.initial!r}, to_write={var.to_write!r}"
if getattr(var, "attrs", None):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't print empty attrs dictionaries

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the change on this line about the dtype , but why shouldn't the empty attrs dict be printed? I think showing it is better

"""Return a pretty repr for Variable"""
return f"Variable(name={var._name!r}, dtype={var.dtype!r}, initial={var.initial!r}, to_write={var.to_write!r}, attrs={var.attrs!r})"
out = f"Variable(name={var._name!r}, dtype={np.dtype(var.dtype).name!r}, initial={var.initial!r}, to_write={var.to_write!r}"
if getattr(var, "attrs", None):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the change on this line about the dtype , but why shouldn't the empty attrs dict be printed? I think showing it is better

Comment thread src/parcels/_repr_utils.py Outdated
time_string = "not_yet_set" if pview.t is None or np.isnan(pview.t) else f"{pview.t:f}"
out = f"P[{pview.particle_id}]: time={time_string}, z={pview.z:f}, y={pview.y:f}, x={pview.x:f}"
vars = [v.name for v in pview._pclass.variables if v.to_write is True and v.name not in ["z", "y", "x", "time"]]
from parcels._core.utils.time import float_to_datelike

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b444b29

Comment on lines -9 to +13
def __init__(self, data, index, pclass):
def __init__(self, data, index, pclass, time_interval=None):
self._data = data
self._index = index
self._pclass = pclass
self._time_interval = time_interval

@VeckoTheGecko VeckoTheGecko Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this state need to be added? just saw the other comment

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Improve ParticleSet.__repr__

2 participants